package hu.tortavar2.client;

import hu.tortavar2.client.mvp.AppPlaceHistoryMapper;
import hu.tortavar2.client.mvp.ContentActivitiMapper;
import hu.tortavar2.client.place.WelcomePlace;
import hu.tortavar2.client.view.layout.AppContentLayout;
import hu.tortavar2.client.view.layout.AppContentLayoutImpl;
import hu.tortavar2.client.view.layout.FooterImpl;
import hu.tortavar2.client.view.layout.SiteMenuImpl;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Tortavar2 implements EntryPoint {

	private Place defaultPlace;
	@Override
	public void onModuleLoad() {
		// TODO Auto-generated method stub
		//defaultPlace = new AddNewProductPlace("newProd");
		defaultPlace = new WelcomePlace();
		//RootPanel.get("wrapper").add(new AppContentLayoutImpl());
		
		loadSite();
		parse();
	}
	
	private void loadSite() {
		ClientFactory clientFactory = GWT.create(ClientFactoryImpl.class);
		EventBus eventBus = clientFactory.getEventBus();
		
		AppContentLayout appWidget = new AppContentLayoutImpl();
		ContentActivitiMapper contentActivitiMapper = new ContentActivitiMapper(clientFactory);
		ActivityManager contentActivityManager = new ActivityManager(contentActivitiMapper, eventBus);
		contentActivityManager.setDisplay(appWidget.getMainPanel());
		
		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		
		PlaceController placeController = clientFactory.getPlaceController();
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);
		
		RootPanel.get("menu").add(new SiteMenuImpl(clientFactory));
		RootPanel.get("wrapper").add((Widget) appWidget.getAppContentLayout());
		RootPanel.get("wrapper").add(new FooterImpl());
		historyHandler.handleCurrentHistory();
	}
	
	public native void parse () /*-{
		$wnd.FB.XFBML.parse();
	}-*/;
}
